
var app = getApp();
// pages/gongxianguzhi/index.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: '',
    nickName: '',
    bossName: "",
    currentBoss: 0,

    total_flag: true, //控制描述显示
    total: '',         //控制显示描述标题
    text: '',          //控制描述文字
    have_calculation: 0,
    icon_text:"",

    //存入当前输入框值
    shareholders_score: {
      founder_responsibility_score: 0,
      responsibility_risk_score: 0,
      startup_ideas_score: 0,
      business_plan_score: 0,
      field_specialization_score: 0,
    },

    startup_ideas_score: '',
    business_plan_score: '',
    field_specialization_score: '',
    responsibility_risk_score: '',
    founder_responsibility_score: '',

    nextPage: "/pages/gongxianpingfen/index?isBack=false",
    //权重
    area1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    color: ["#7bb5d5", "#feb45f", "#b2df62", "#b8bae0", "#9ccfc8"],
  },

  /**
    * 生命周期函数--监听页面加载
    */
  onLoad: function (options) {
    if (wx.getStorageSync('userInfo')) {
      this.setData({
        avatarUrl: JSON.parse(wx.getStorageSync('userInfo')).avatarUrl,
        nickName: JSON.parse(wx.getStorageSync('userInfo')).nickName,
      })
    }
    this.setData({
      have_calculation: app.globalData.have_calculation
    });
    if (app.globalData.have_calculation == 1) {
      this.setData({
        icon_text: "报告记录"
      })
    } else {
      this.setData({
        icon_text: "返回首页"
      })
    }

    //-------------------------渲染页面------------------
    //得到缓存的所有股东
    var shareholders_list = app.globalData.shareholders_list;
    //得到当前bossid
    var currentBoss = app.globalData.currentBoss;

    //如果当前是最后一个boss则修改跳转路径 转到 是否查看结果
    // console.log(currentBoss);
    if (currentBoss == shareholders_list.length - 1) {
      this.setData({
        nextPage: "/pages/IsUseContribution/index",
      });
    }

    // console.log(options.isBack+"")
    if (options.isBack == "true") {

      // console.log(app.globalData.shareholders_score_list)
      // console.log("currentBoss:" + currentBoss);
      // console.log()
      var data = app.globalData.shareholders_score_list[currentBoss + 1]
      this.setData({
        shareholders_score: data,
      });
    }

    var bosstName = shareholders_list[Number(currentBoss)].shareholders_name;

    this.setData({
      //从缓存中载入文字描述文本
      startup_ideas_score: app.globalData.shareholders_score.startup_ideas_score,
      business_plan_score: app.globalData.shareholders_score.business_plan_score,
      field_specialization_score: app.globalData.shareholders_score.field_specialization_score,
      responsibility_risk_score: app.globalData.shareholders_score.responsibility_risk_score,
      founder_responsibility_score: app.globalData.shareholders_score.founder_responsibility_score,
      //当前股东名称
      bossName: bosstName,
      currentBoss: currentBoss
    });
    this.$wuxToptips = app.Wux().$wuxToptips;
  },

  bindPickerChange: function (e) {
    var shareholders_score = this.data.shareholders_score;
    switch (e.currentTarget.id) {
      case "1":
        shareholders_score.startup_ideas_score = e.detail.value;
        this.setData({
          shareholders_score: shareholders_score
        });
        return;
      case "2":
        shareholders_score.business_plan_score = e.detail.value;
        this.setData({
          shareholders_score: shareholders_score
        });
        return;
      case "3":
        shareholders_score.field_specialization_score = e.detail.value;
        this.setData({
          shareholders_score: shareholders_score
        });
        return;
      case "4":
        shareholders_score.responsibility_risk_score = e.detail.value;
        this.setData({
          shareholders_score: shareholders_score
        });
        return;
      case "5":
        shareholders_score.founder_responsibility_score = e.detail.value;
        this.setData({
          shareholders_score: shareholders_score
        });
        return;
      default:
        break;
    }
  },

  //页面跳转
  submitForm: function (e) {
    // console.log(e)
    //当前股东id
    var currentBoss = Number(this.data.currentBoss);

    app.globalData.shareholders_score_list[currentBoss + 1] = e.detail.value;
    //把所有数据存入 entity中
    app.globalData.entity.shareholders_score_list = JSON.stringify(app.globalData.shareholders_score_list);

    if (!this.chackValue(app.globalData.shareholders_list, e.detail.value)) {

      var hideToptips = this.$wuxToptips.show({
        timer: 3000,
        text: '股东权重乘以评分总和不能为0',
        success: () => console.log('toptips')
      });
      return false;
    }

    //bossId加一
    app.globalData.currentBoss = currentBoss + 1;

    wx.redirectTo({
      url: this.data.nextPage,
    });
  },

  //验证总和 传入股东列表和股东贡献评分法权重值
  chackValue: function (shareholders_list, shareholders_score) {

    // console.log(111);
    var count = 0;  //总值
    var founder_responsibility_score = 0,
      responsibility_risk_score = 0,
      startup_ideas_score = 0,
      business_plan_score = 0,
      field_specialization_score = 0,

      //取出权重页面分配值
      startup_ideas = app.globalData.contribution_score_weight_list.startup_ideas,
      business_plan = app.globalData.contribution_score_weight_list.business_plan,
      field_specialization = app.globalData.contribution_score_weight_list.field_specialization,
      responsibility_risk = app.globalData.contribution_score_weight_list.responsibility_risk,
      founder_responsibility = app.globalData.contribution_score_weight_list.founder_responsibility;



    founder_responsibility_score = shareholders_score.founder_responsibility_score;
    responsibility_risk_score = shareholders_score.responsibility_risk_score;
    startup_ideas_score = shareholders_score.startup_ideas_score;
    business_plan_score = shareholders_score.business_plan_score;
    field_specialization_score = shareholders_score.field_specialization_score;

    count += startup_ideas * startup_ideas_score;
    count += business_plan * business_plan_score;
    count += field_specialization * field_specialization_score;
    count += responsibility_risk * responsibility_risk_score;
    count += founder_responsibility * founder_responsibility_score;

    // console.log(startup_ideas * startup_ideas_score);
    // console.log(business_plan * business_plan_score);
    // console.log(field_specialization * field_specialization_score);
    // console.log(responsibility_risk * responsibility_risk_score);
    // console.log(founder_responsibility * founder_responsibility_score);

    if (count == 0) {

      return false;
    }
    return true;

  },


  //返回上一个页面
  navigateBack: function () {

    //如果当前boss 大于0 说明之前还有页面 
    if (this.data.currentBoss > 0) {
      app.globalData.currentBoss = this.data.currentBoss - 1;
      wx.redirectTo({
        url: "/pages/gongxianpingfen/index?isBack=true",
      });
      // console.log(app.globalData.currentBoss);
    } else {
      wx.navigateBack({

      })
    }
  },
  show: function (e) {

    var that = this;
    switch (e.currentTarget.id) {
      case "1":
        that.setData({
          total: '创业点子',
          text: that.data.startup_ideas_score,
        });
        break;
      case "2":
        that.setData({
          total: '商业计划书',
          text: that.data.business_plan_score,
        });
        break;
      case "3":
        that.setData({
          total: '领域专业性',
          text: that.data.field_specialization_score,
        });
        break;
      case "4":
        that.setData({
          total: '担当及风险',
          text: that.data.responsibility_risk_score,
        });
        break;
      case "5":
        that.setData({
          total: '创始人责任',
          text: that.data.founder_responsibility_score,
        });
        break;
      default:
        break;
    }
    that.setData({ total_flag: false })
  },

  icon_redirectTo: function () {
    var that = this;
    if (that.data.have_calculation == 1) {
      wx.redirectTo({
        url: '/pages/history/index',
      })
    } else {
      wx.redirectTo({
        url: '/pages/index/index',
      })
    }
  },
  hide: function () {

    this.setData({ total_flag: true })
  },
})